package com.sast;

import cn.hutool.core.util.XmlUtil;
import com.sast.device.XmlClass;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;


public class Main {
    public static XmlClass readXml(File xmlFile) {
        //新建一个数据模型
        XmlClass xmlClass = new XmlClass();
        //读取xml文件
        Document document = XmlUtil.readXML(xmlFile);
        Element device = XmlUtil.getElementByXPath("//设备", document);
        //填入设备名称
        xmlClass.setName(device.getAttribute("value"));
        //填入配置信息
        Element configElement = XmlUtil.getElement(device, "配置");
        List<Element> configurations = XmlUtil.getElements(configElement, "配置项");
        for (Element configuration : configurations) {
            XmlClass.DeviceConfiguration config = new XmlClass.DeviceConfiguration();
            config.setName(configuration.getAttribute("name"));
            config.setValue(configuration.getAttribute("value"));
            xmlClass.getConfigurations().add(config);
        }
        //填入指令信息
        Element commandsElement = XmlUtil.getElement(device, "指令");
        List<Element> commands = XmlUtil.getElements(commandsElement, "控制指令");
        for (Element commandEle : commands) {
            XmlClass.DeviceCommand cmd = new XmlClass.DeviceCommand();
            cmd.setName(commandEle.getAttribute("name"));
            cmd.setValue(commandEle.getAttribute("value"));
            cmd.setDescription(commandEle.getAttribute("description"));
            cmd.setAction(commandEle.getAttribute("action"));
            //如果有参数
            if(commandEle.hasChildNodes()){
                List<Element> args = XmlUtil.getElements(commandEle,"参数");
                for(Element argEle :args){
                    XmlClass.DeviceCommand.Arg arg = new XmlClass.DeviceCommand.Arg();
                    arg.setName(argEle.getAttribute("name"));
                    cmd.getArgs().add(arg);
                }
            }
            xmlClass.getCommands().add(cmd);
        }
        return xmlClass;

    }

    public static void generateCode(String filepath, XmlClass xmlClass) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        configuration.setDirectoryForTemplateLoading(new File(filepath));
        // 设置模板文件使用的字符集
        configuration.setDefaultEncoding("utf-8");
        //调整输出数字格式 2023 取代 2,023
        configuration.setNumberFormat("0.######");
        // 加载模板
        Template template = configuration.getTemplate("template2.py.ftl");

        //生成导出
        Writer writer = new FileWriter("out.py");
        template.process(xmlClass, writer);
        writer.close();
    }

    public static void main(String[] args) throws IOException, TemplateException {
        XmlClass xmlClass = Main.readXml(new File("src/main/resources/xml/DPO2024.xml"));
        generateCode("src/main/resources/templates", xmlClass);
    }
}